package model.commands.flow;

import model.commands.AbstractCommand;

public class Wait extends AbstractCommand {
	private static final int WAIT_FRACTION_TIME = 1000; //in ms
	private Integer ms;

	@Override
	public void execute() {
		try {
			//splits in calls of 60 seconds or less
			int iterations = ms/WAIT_FRACTION_TIME; 
			if(iterations>0){
				for(int i=0;i<iterations;i++){
					robot.delay(WAIT_FRACTION_TIME);
					while(pause && !stop){
						synchronized (this) {
							try {
								wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
					if(stop){
						break;
					}
				}
			}
			//waits the last NO fraction time
			if(!stop){
				robot.delay(ms%WAIT_FRACTION_TIME);
			}
			propertyChangeSupport.firePropertyChange(EXECUTION_FINISHED, null, this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void setParams(Object[] params) {
		ms = (Integer)params[0];
	}
	
	public Integer getMs() {
		return ms;
	}

}
